package org.ihelpuoo.orm.handler.conn;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;

import org.ihelpuoo.lang.ObjectUtils;
import org.ihelpuoo.orm.ObjectRule;
import org.ihelpuoo.orm.filter.FieldFilter;
import org.ihelpuoo.orm.handler.ObjectProcessor;
import org.ihelpuoo.orm.handler.FieldHandler;

public class ConnSaveArrayFieldHandler extends FieldHandler
{

	public ConnSaveArrayFieldHandler(ObjectProcessor beanHandler)
	{
		super(beanHandler);
	}

	@Override
	public void processWithoutExceptionHandle(ObjectRule r, Field f, Object conn, Object obj, FieldFilter actived, FieldFilter locked, String prefix)
			throws Exception
	{
		Method getter = ObjectUtils.getGetter(obj.getClass(),f);
		Object[] subs = (Object[])getter.invoke(obj,(Object[])null);
		if (null == subs || subs.length == 0)
			return;
		ObjectRule subRule = processor.getManual().getRule(subs[0].getClass());
		for (int i = 0; i < subs.length; i++)
		{
			subRule.checkDatabaseConfig((Connection)conn,subs[i]);
			if (subRule.isNewInDB(subs[i]))
				processor.insert(subRule,conn,subs[i],null,null);
			else
				processor.update(subRule,conn,subs[i],null,null);
		}
	}

}
